package rudia.net.user.web;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import rudia.org.core.RuMap;
import rudia.org.core.Rutil;
import rudia.org.user.service.LoginService;
import rudia.org.user.service.UserService;

@Controller
public class LoginController {

	@Autowired
	private UserService userService;

	@Autowired
	private LoginService loginService;

	@RequestMapping("/login/form")
	public String form(HttpServletRequest request) {

		return "rudia/user/login/form";
	}

	@RequestMapping("/login/login")
	public String login(HttpServletRequest request) {

		return "rudia/user/login/login";
	}

	@RequestMapping("/login/loginAction")
	public String loginAction(HttpServletRequest request, RuMap<String, Object> rumap) {
		RuMap user = loginService.loginUser(rumap);

		if (user != null && user.get("user_id") != null && !user.get("user_id").equals("")) {

			request.getSession().setAttribute("loginUser", user);

			return "redirect:http://localhost:8080/";

		} else {
			rumap.put("errorMsg", Rutil.getMsg("fail.common.login"));
			return "rudia/user/login/form";
		}

	}
	@RequestMapping("/login/logoutAction")
	public String logoutAction(HttpServletRequest request, RuMap<String, Object> rumap) {
		RuMap user = loginService.loginUser(rumap);
		
		if (user != null && user.get("user_id") != null && !user.get("user_id").equals("")) {
			
			request.getSession().setAttribute("loginUser", user);
			
			return "redirect:http://localhost:8080/";
			
		} else {
			rumap.put("errorMsg", Rutil.getMsg("fail.common.login"));
			return "rudia/user/login/form";
		}
		
	}

}
